home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Interactive Web Graphics with Shout 3D
/
Interactive Web Graphics With Shout 3D.iso
/
pc
/
Code
/
Chapter Code.exe
/
Chapter08
/
RotateHPRPanel.java
< prev
next >
Wrap
Text File
|
2000-06-28
|
3KB
|
124 lines
package applets;
import shout3d.*;
import shout3d.core.*;
import shout3d.math.*;
public class RotateHPRPanel extends Shout3DPanel implements DeviceObserver {
//Node in scene to rotate
Transform boxTrans;
//pixels on screen
int pixelStartX;
int pixelStartY;
int pixelEndX;
int pixelEndY;
//to store and compute angles
float [] eulers = new float [3];
float [] axisAngle = new float [4];
Quaternion q = new Quaternion();
public RotateHPRPanel(Shout3DApplet applet){
super(applet);
}
public void customInitialize() {
addDeviceObserver(this,"MouseInput", null);
boxTrans = (Transform) getNodeByName("Box01");
//get starting rotation value from scene
axisAngle = boxTrans.rotation.getValue();
//put this rotation into the quaternion
q.setAxisAngle(axisAngle);
//get it back out as eulers
q.getEulers(eulers);
}
protected void finalize() {
removeDeviceObserver(this,"MouseInput");
}
public boolean onDeviceInput(DeviceInput di, Object userData) {
MouseInput mi = (MouseInput) di;
switch (mi.which){
case MouseInput.DOWN:
pixelStartX = mi.x;
pixelStartY = mi.y;
return true;
case MouseInput.DRAG:
//if left button used
if(mi.button == 0) {
pixelEndX = mi.x;
pixelEndY = mi.y;
int dragDistanceX = pixelEndX - pixelStartX;
int dragDistanceY = pixelEndY - pixelStartY;
//convert drag to rotation
//at 50 pixels per radian (57.2 degrees)
float headingDelta = dragDistanceX/50f;
float pitchDelta = dragDistanceY/50f;
//compute new heading and pitch
eulers[0] = eulers[0] + headingDelta;
eulers[1] = eulers[1] + pitchDelta;
//Convert from Eulers to AxisAngle
// using the Quaternion
q.setEulers(eulers);
q.getAxisAngle(axisAngle);
//set the Transform to
//updated axis angle values
boxTrans.rotation.setValue(axisAngle);
pixelStartX = pixelEndX;
pixelStartY = pixelEndY;
return true;
}//end of 0 if
//if right button used
if(mi.button == 1) {
pixelEndY = mi.y;
int dragDistanceY = pixelEndY - pixelStartY;
float rollDelta = dragDistanceY/50f;
eulers[2] = eulers[2] + rollDelta;
q.setEulers(eulers);
q.getAxisAngle(axisAngle);
boxTrans.rotation.setValue(axisAngle);
pixelStartY = pixelEndY;
return true;
}//end of 1 if
}//end of switch
return false;
}
}//end of class